﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iTextSharp.text;
using iTextSharp.text.pdf;
using IPSWWA.Models;

namespace IPSWWA.Controllers
{
    public class WWAController : Controller
    {
        //
        // GET: /WWA/

        public ActionResult Index()
        {
            return View();
        }

        /*Here's the working controllers
         * Working, Workingbyid as well as the partialviews for working
         */

        public ActionResult Working()
        {
            IUser User = new UserModels();
            IWorkingLists Workingview;
            if (User.Role == /*Redacted*/)
            {
                Workingview = new DeltaWorkingObject();
            }
            else
            {
                Workingview = new AllWorkingObject();
            }
            return View(Workingview.Working);
        }

		
        public ActionResult WorkingbyID(int id)
        {
            IUser User = new UserModels();
            WorkingObject Working = new WorkingObject(id);
            return View(Working);
        }

        /*Waiting views are below here 
         * including waiting, waitingbyid and partialviews for waiting
         */

        public ActionResult Waiting()
        {
            IUser User = new UserModels();
            AllWaitingObject Waiting = new AllWaitingObject();
            return View(Waiting.Waiting);
        }

        public ActionResult Waitingbyid(int id)
        {
            IUser User = new UserModels();
            WaitingObject Waiting = new WaitingObject(id);
            return View(Waiting);
        }

        //Below here are the httpposts for WWA as well as one method that returns files

        [HttpPost]
        public ActionResult UploadFile(int id, FormCollection form, HttpPostedFileBase File)
        {
            IUser User = new UserModels();
            IUploadFactory Upload = new UploadFactory();
            if (File == null)
            {
                return RedirectToAction("Waiting");
            }
            if (File.ContentLength > 0)
            {
                IWWAUpload fileupload = Upload.CommandFactory(id, form, File);
                fileupload.Upload();
            }
            return RedirectToAction("Waiting");
        }

        [HttpPost]
        public ActionResult Update(int id, FormCollection form)
        {
            IWWAFactory Factory = new WWAUpdateFactory();
			//This is the spot where I should use an invoker, but I figure since this is web based I can't easily enough store past commands to undo
            WWACommandpattern Command = Factory.CommandFactory(id, form);
            Command.Execute();
            return Redirect(Request.UrlReferrer.ToString());
        }

        [HttpPost]
        public ActionResult Fax01Record(int id)
        {
            IUser user = new UserModels();
            WorkingObject record = new WorkingObject(id);
            DrugPDFFilefactory filefactory = new DrugPDFFilefactory(record);
            PDFfaxforms form = filefactory.Factory();
            form.Loadvalues(PDFFilestring.Factory());
            IPSFaxEmail email = new IPSFaxEmail(user.Name, record.Doctor.Drfax, "01 Fax", "", form.Returnfileinmemory());
            email.Send();
            return Redirect(Request.UrlReferrer.ToString());
        }
    }
}